package qiurank;

import app.*;

/**
 * Created by Administrator on 2016/1/4.
 */
public class Dothread extends Thread {
    private boolean isrun;
    private UrlManageInterface urlManage;
    //private TouTiaoHandle handle;
    private PaHandle handle;
    private int index=0;
    private boolean isout;
    private ThreadManage _threadManage;
    public Dothread(UrlManageInterface _urlManage,ThreadManage __threadManage){
        isrun=true;
        isout=true;
        _threadManage=__threadManage;
        if(_urlManage!=null) {
            urlManage = _urlManage;
        }
        //handle=new TouTiaoHandle();
        try {

            handle=(PaHandle)HandleFactory.createHandle();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    public void setIndex(int i){
        index=i;
    }
    @Override
    public void run() {
        while (isout) {
            while (isrun) {
                //从待爬取列表中取出一个url
                UrlObject noPa=null;
                synchronized (Dothread.class) {
                    noPa = urlManage.getNoUrl();
                    if (noPa != null) {
                        urlManage.setPa(noPa);
                        PaFather pa=(PaFather)PaFactory.createPa(noPa,urlManage);
                        handle.handle(pa, _threadManage);
                    } else {
                        Log.show("待爬取url为空，列表中没有待爬取url");
                    }
                }
                //Log.console("xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx线程完毕xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx");

            }
        }

    }

    /**
     * 结束线程
     */
    public void finish(){
        isout=false;
        isrun=false;
    }
    //暂停线程
    public void pause(){
        isrun=false;
    }
    //重新启动线程
    public void restart(){
        isrun=true;
    }

}
